<?php
/**
 * Created by PhpStorm.
 * User: kalv1n
 * Date: 2017/1/6
 * Time: 下午4:55
 */
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;

class StudentController extends Controller
{
    public function showInfo(){
        phpinfo();
//        return __METHOD__;
        //查询
//        $students=DB::select('select * from student');
        //插入
//        $flag=DB::insert('insert into student(name,age) values(?,?)',['吴伟',18]);
        //更新
//        $flag=DB::update('update student set age =? where name =?',['20','jack']);
//        $flag=DB::delete('delete  from student where id = ? ',['1']);
//        var_dump($flag);
//        echo '<pre>';
//        var_dump($students);
//        return view('Student/showInfo',['studnet'=>$students]);

    }
    public function query1(){
        //查询构造器 之 插入
//        $bool=DB::table('student')->insert(
//            ['name'=>'mary','age'=>18]
//        );

//        $bool=DB::table('student')->insertGetId(
//            ['name'=>'sean','age'=>18]
//        );

        $bool=DB::table('student')->insert([
            ['name'=>'吴嘉伟','age'=>18],
            ['name'=>"kalv1n",'age'=>12],
        ]);
        var_dump($bool);
    }
    //查询构造器之更新
    public function query2(){
//            $bool=DB::table('student')->where('id',2)->update(
//                ['age'=>58]
//            );
//        $bool=DB::table('student')->increment('age');
//        $bool=DB::table('student')->increment('age',3);
//        $bool=DB::table('student')->where('id',2)->decrement('age',3);
        $bool=DB::table('student')->where('id',2)->decrement('age',3,['name'=>'吴嘉伟']);
        var_dump($bool);
    }
    //查询构造器之删除
    public function query3(){
//        $bool= DB::table('student')->where('id',7)->delete();
//        $bool= DB::table('student')->where('id','>=',5)->delete();
          $bool=DB::table('student')->truncate();
    }
    //查询构造器之查询
    public function query4(){
//        $res=DB::table('student')->get();
//        $res=DB::table('student')->orderby('id','desc')->first();
//        $res=DB::table('student')->where('id',1)->first();
//        $res=DB::table('student')->whereRaw('id=? and name=?',[2,'kalv1n'])->first();
          //pluck
//        $res=DB::table('student')
//
//            ->pluck('name');
        //select
//                $res=DB::table('student')->select('id','name','age')->get();
        //chuck
        $res=DB::table('student')->chunk(1,function($res){
            var_dump($res);
            return false;
        });

    }
    //聚合函数
    public function  query5(){
//            $res = DB::table('student')->count();
//            $res = DB::table('student')->max('age');
//            $res = DB::table('student')->min('age');
//            $res = DB::table('student')->avg('age');
            $res = DB::table('student')->sum('age');

            dd($res);
    }
    public function orm1(){
        // all()
//        $students=Student::all();
        // find()
//        $students=Student::find(1);
        // findOrFail
//        $students=Student::findOrFail(3);
        //
//        $students=Student::findOrFail(3);
//        dd($students);

//        $res=Student::get();
//        $res=Student::where('id','>','0')->get();
        // chunk
        echo '<pre>';
        $res=Student::chunk(1,function($student){
                var_dump($student);
        });
        dd($res);
    }
    public function orm2(){
//        使用模型新增数据
//        $student = new Student();
//        $student->name = 'sean1';
//        $student->age=30;
//        $student->save();
        $student=Student::find(5);
//        var_dump( $student->created_at);
//          dd($student->created_at);

//          echo $student->created_at;
//        foreach( $student->created_at as $k=>$v){
//                var_dump($k,$v);
//        }
        //使用模型的create方法新增数据
//        $student=Student::create(
//            ['name'=>'ben','age'=>'18']
//        );
//        var_dump($student);
        //firstOrCreate()
//        $student=Student::firstOrCreate(
//            ['name'=>'bens']
//        );
        //firstOnNew()
//        $student=Student::firstOrNew(
//            ['name'=>'bensss']
//        );

        dd($student);

    }
    public function agebig()
    {
       return 'here is bigger age';
    }
    public function agesmall(){
        return 'here is smaller age';
    }
}